package api;

import benchmarks.tpcc.mongo.TPCCBenchMongo;
import benchmarks.tpcc.mysql.TPCCBenchMySQL;
import benchmarks.tpcc.redis.TPCCBenchRedis;
import benchmarks.tpch.mongo.TPCHBenchMongo;
import benchmarks.tpch.mysql.TPCHBenchMySQL;
import benchmarks.tpch.redis.TPCHBenchRedis;

public class BenchmarkFactory {
    public BenchmarkModule createBenchmark(Configuration config) throws UndefinedConfigException{
        String benchmarkName = config.getBenchmark().toLowerCase();
        String dbName = config.getDb_name().toLowerCase();
        BenchmarkModule benchmarkModule = null;

        //TPC-C
        if (benchmarkName.equals("tpcc") || benchmarkName.equals("tpc-c")) {
            if (dbName.equals("mysql")) {
                benchmarkModule = new TPCCBenchMySQL(config);
            }
            else if (dbName.equals("mongo") || dbName.equals("mongodb")) {
                benchmarkModule = new TPCCBenchMongo(config);
            }
            else if (dbName.equals("redis")) {
                benchmarkModule = new TPCCBenchRedis(config);
            }
            else {
                throw new UndefinedConfigException("No database named " + dbName);
            }
        }

        //TPC-H
        else if (benchmarkName.equals("tpch") || benchmarkName.equals("tpc-h")) {
            if (dbName.equals("mysql")) {
                benchmarkModule = new TPCHBenchMySQL(config);
            }
            else if (dbName.equals("mongo") || dbName.equals("mongodb")) {
                benchmarkModule = new TPCHBenchMongo(config);
            }
            else if (dbName.equals("redis")) {
                benchmarkModule = new TPCHBenchRedis(config);
            }
            else {
                throw new UndefinedConfigException("No database named " + dbName);
            }
        }
        else {
            throw new UndefinedConfigException("No benchmark named " + benchmarkName);
        }

        return benchmarkModule;
    }

    public class UndefinedConfigException extends Exception{
        public UndefinedConfigException(String message) {
            super(message);
        }
    }
}
